#wave-email.py

from google.appengine.ext import db
import EmailStore
#Wave APIS
from waveapi import events
from waveapi import model
from waveapi import robot
from waveapi import document


def OnBlipSubmit(properties, context):
    emails = db.GqlQuery("SELECT * FROM EmailStore")
    emailText = ""
    root_wavelet = context.GetRootWavelet()
    for currEmail in emails:
        emailText = ('From: %s \nTo: %s \nSubject: %s \nMessage: \n%s' %
        (currEmail.sender,currEmail.recipient, currEmail.subject, currEmail.message))
        root_wavelet.CreateBlip().GetDocument().SetText(emailText)

def OnRobotAdded(properties, context):  
    """Invoked when the robot has been added."""
    root_wavelet = context.GetRootWavelet()
    root_wavelet.CreateBlip().GetDocument().SetText("Welcome to Wave Email!")

if __name__ == '__main__':
    waveemail = robot.Robot('WaveEmail',
                         image_url='http://wave-email.appspot.com/public/logo.png',
                         version='1.04',
                         profile_url='http://wave-email.appspot.com')
    waveemail.RegisterHandler(events.WAVELET_SELF_ADDED, OnRobotAdded)
    waveemail.RegisterHandler(events.BLIP_SUBMITTED, OnBlipSubmit)
    waveemail.Run(debug=True)
